package com.fy.login;

import java.io.IOException;
import java.io.ObjectStreamException;
import java.io.Serializable;
import java.util.Objects;

/**
 * @author zhufeifei 2022/8/9
 **/

public class BaseMetadata implements Serializable {
    private String username;
    private Role role;

    public BaseMetadata() {

    }

    public Role getRole() {
        return role;
    }

    public void setRole(Role role) {
        this.role = role;
    }
    public BaseMetadata(Role role, String username) {
        this.username = username;
        this.role = role;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @Override
    public String toString() {
        return "BaseMetadata{" + "username='" + username + '\'' +
                ", role=" + role +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        BaseMetadata metadata = (BaseMetadata) o;
        return Objects.equals(username, metadata.username) && role == metadata.role;
    }

    @Override
    public int hashCode() {
        return Objects.hash(username, role);
    }


    public void writeObject(java.io.ObjectOutputStream out) throws IOException {
        out.defaultWriteObject();
        out.writeUTF(username);
        out.writeUTF(role.toString());
        out.flush();
    }

    public void readObject(java.io.ObjectInputStream in)
            throws IOException, ClassNotFoundException {
        in.defaultReadObject();
        this.username = in.readUTF();
        this.role = Role.of(in.readUTF());
    }

    public void readObjectNoData()
            throws ObjectStreamException {
        this.role = Role.NONE;
        this.username = "NULL";
    }

}
